/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.rpc.base;

import org.openislands.oi.error.RemoteCallException;

import java.io.IOException;

public interface RoutePolicy {
    /***
     * Called when all routing requests of the node failed
     * @param executor current request executor
     * @return request default result
     * @throws IOException If no default result, can throw IOException
     */
    Object allExecutionFailed(Executor<?> executor) throws IOException;

    /***
     * Called when each node's route call failed
     * @param executor  current request executor
     * @param route     current request route
     * @param nextRoute next request route
     * @return next call route
     * @throws IOException If no need to continue request, can throw IOException
     */
    RouteNode executionFailed(Executor<?> executor, RouteNode route, RouteNode nextRoute) throws IOException;

    class DefaultRoutePolicy implements RoutePolicy {
        @Override
        public Object allExecutionFailed(Executor<?> executor) throws IOException {
            throw new RemoteCallException("request all network route failed");
        }

        @Override
        public RouteNode executionFailed(Executor<?> executor, RouteNode route, RouteNode nextRoute) {
            return nextRoute;
        }
    }
}
